package net.jueb.juebCore.queue;

import net.jueb.util4j.hotSwap.classFactory.IScript;
import net.jueb.util4j.queue.order.OrderTaskQueue.Task;
import net.jueb.util4j.queue.order.OrderTaskQueueGroup;

public class OrderScriptQueueGroup extends OrderTaskQueueGroup{

	private static final OrderScriptQueueGroup jobGroup=new OrderScriptQueueGroup();
	private OrderScriptQueueGroup() {
		super();
	}
	
	public void addScript(int index,final IScript script)
	{
		if(script==null)
		{
			return;
		}
		String key="ScriptQueue-"+index;
		put(key,new Task() {
			@Override
			public void action() throws Throwable {
				try {
					script.run();
				} catch (Exception e) {
					log.error(e.getMessage(),e);
					throw e;
				}
				
			}
			@Override
			public String name() {
				return script.getClass().getName();
			}
		});
	}
	public void addScript(String queuekey,final IScript script)
	{
		if(script==null)
		{
			return;
		}
		String key="ScriptQueue-"+queuekey;
		put(key,new Task() {
			@Override
			public void action() throws Throwable {
				try {
					script.run();
				} catch (Exception e) {
					log.error(e.getMessage(),e);
					throw e;
				}
			}
			@Override
			public String name() {
				return script.getClass().getName();
			}
		});
	}
	
	public static OrderScriptQueueGroup getInstance()
	{
		return jobGroup;
	}
}
